package cdc.retrofit.method;

import java.io.IOException;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class Request {

	public void requestContributor() {
		Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.API_URL)
				.addConverterFactory(GsonConverterFactory.create()).build();

		// Create an instance of our GitHub API interface.
		Api api = retrofit.create(Api.class);

		// Create a call instance for looking up Retrofit contributors.
		Call<List<Contributor>> call = api.contributors("square", "retrofit");

		// Fetch and print a list of the contributors to the library.
		List<Contributor> contributors = null;
		try {
			contributors = call.execute().body();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (Contributor contributor : contributors) {
			System.out.println(contributor.getLogin() + " (" + contributor.getContributions() + ")");
		}
	}
	/**
	 * https://api.github.com/repos/square/retrofit/contributors
	 */
	public void requestContributorEnqueue() {
		Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.API_URL)
				.addConverterFactory(GsonConverterFactory.create()).build();

		// Create an instance of our GitHub API interface.
		Api api = retrofit.create(Api.class);

		// Create a call instance for looking up Retrofit contributors.
		Call<List<Contributor>> call = api.contributors("square", "retrofit");

		// Fetch and print a list of the contributors to the library.
		call.enqueue(new Callback<List<Contributor>>() {
			
			@Override
			public void onResponse(Call<List<Contributor>> call, Response<List<Contributor>> response) {
				// TODO Auto-generated method stub
				List<Contributor> contributors =response.body();
				System.out.println(call.request().url().toString());
				for (Contributor contributor : contributors) {
					System.out.println(contributor.getLogin() + " (" + contributor.getContributions() + ")");
				}
			}
			
			@Override
			public void onFailure(Call<List<Contributor>> call, Throwable t) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}

}
